package cn.bqjr.eily.samples.common.kaptcha;

import cn.bqjr.eily.shiro.spring.boot.entity.ShiroProperties;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration
public class KaptachaConfiguration {

    @Autowired
    private ShiroProperties shiroProperties;

    /**
     * 验证码组件初始化
     *
     * @return
     */
    @Bean
    public DefaultKaptcha defaultKaptcha() {
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        Properties properties = new Properties();
        properties.setProperty("kaptcha.image.width", "120");
        properties.setProperty("kaptcha.image.height", "60");
        properties.setProperty("kaptcha.textproducer.char.length",
                String.valueOf(shiroProperties.getBase().getVerifyCode().getCharLength()));

        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
}
